<?php	
	//include_once('verifica.php');
	include_once('config/class.pdo.php');
    
	//print_r($_POST[id]);
	if(isset($_POST['id'])) {
		$pdo = new DB;	
		$pdo-> conexao();
		
		$Query = 'SELECT 
					idCliente,
					assunto,
					date_format(dataAbertura, "%d/%m/%Y %T") as dataAbertura,					
					nome,
					login,
					idAtendimento
				  FROM atendimento 
				  LEFT JOIN entidade ON entidade.idEntidade = atendimento.idCliente
				  LEFT JOIN usuario ON usuario.idusuario = atendimento.idusuario
		WHERE idAtendimento = '.$_POST['id'].'';
		//print($Query);
		$result = $pdo->oneReg($Query);				
		//print_r($result);
	} 			
?>
<!doctype html>
<html lang="en">
	<head>
		<meta charset="utf-8" />
		<link rel="stylesheet" href="css/jquery-ui.css" />
		<link rel="stylesheet" href="css/style.css" />
		<link rel="stylesheet" href="js/DataTables-1.10.0/media/css/jquery.dataTables.css" />		
		<script type="text/javascript" src="js/jquery-1.9.1.js" ></script>
		<script type="text/javascript" src="js/jquery-ui.js" ></script>	
		<script type="text/javascript" src="js/jquery.maskedinput.js" ></script>		
		<script type="text/javascript" src="js/jquery.validate.js" ></script>
		<script type="text/javascript" src="js/DataTables-1.10.0/media/js/jquery.dataTables.js" ></script>
		<script type="text/javascript" src="js/funcoes.js"></script>			
		<script type="text/javascript" src="js/jsCadAtendimento.js"></script>		
		<script type="text/javascript" src="js/jsCadHistorico.js"></script>		
		<script type="text/javascript" src="js/jsConsultaHistorico.js"></script>		
	</head>	
	<body style="font-size:10px;">
		<form name="frmAtendimento" id="frmAtendimento" method="POST">
			<div id="divAtendimento" class="divAtendimento">
				<fieldset class="backFieldset">
					<legend> <b> Atendimento Inicial </b> </legend>
					<table class="table">
						<?php 
							if(isset($_POST['id'])) {
								echo '<tr>';
								echo '<td width=80> Código: </td>';
								echo '<td colspan=0> <input type="text" name="ca_id" id="ca_id" value="'.$result['idAtendimento'].'" readonly />';
								echo '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Cadastro: &nbsp;&nbsp;';
								echo '<input type="text" name="ca_dataAbertura" id="ca_dataAbertura" value="'.$result['dataAbertura'].'" readonly /> </td>';
								echo '</tr>';
							}
						?>
						<tr>
							<td> Assunto: </td>
							<td colspan=3> <input type="text" name="ca_assunto" id="ca_assunto" class="input-xxlarge" title="Campo ASSUNTO deve ser preenchido." value="<?php echo utf8_encode(@$result['assunto']); ?>" required /> </td>
						</tr>
						<tr>
							<td> Cliente: </td>
							<td colspan=3> <input type="text" name="ca_cliente" id="ca_cliente" class="input-xxlarge" title="Campo CLIENTE deve ser preenchido."  value="<?php echo utf8_encode(@$result['nome']); ?>" required /> <input type="hidden" name="ca_codCliente" id="ca_codCliente" title="Campo CLIENTE deve ser preenchido corretamente." value="<?php echo @$result['idCliente']; ?>" required /> </td>
						</tr>						
						</tr>
						<?php
							echo '<tr>';
								if(isset($_POST['id'])) {
									echo '<td colspan=2> <button type="button" name="voltar" id="voltar" onclick="loadPage(15);" class="btn btn-primary" > <i class="icon-arrow-left"></i> Voltar </button> <input type="hidden" id="controle" value="atualizar"> </td>';
								} else {
									echo '<td colspan=2> <button type="submit" name="gravar" class="btn btn-success"> <i class="icon-ok"></i> Gravar </button>'; 
									echo '    <button type="button" name="cancelar" onclick="apagaForm()" class="btn btn-danger" > <i class="icon-ban-circle"></i> Cancelar </button>';
									echo '    <button type="button" name="sair" id="sair" onclick="loadPage(1);" class="btn btn-primary" > <i class="icon-home"></i> Sair </button> <input type="hidden" id="controle" value="gravar"> </td>';
								}
							echo '</tr>';
						?>
					</table>
				</fieldset>
			</div>
		</form>
		<?php
            if(isset($_POST['id'])) {
        ?>			
                <div id="divAtendimento" class="divAtendimento">
                    <fieldset>
                        <legend> <b> Histórico </b> </legend>
                        <table id="historico" class="display" cellspacing="0" width="100%">
                            <thead>
                                <tr>
                                    <td> Data </td>
                                    <td> Observações </td>
                                </tr>
                            </thead>
                            <tbody>
                                <?php
                                    $pdo = new DB;	
                                    $pdo-> conexao();
                                    
                                    $Query = 'SELECT 
                                                idhistorico,
                                                observacoes,
                                                date_format(dataMovimentacao, "%d/%m/%Y %T") as dataMovimentacao
                                              FROM historico
                                    WHERE fk_idAtendimento = '.$_POST['id'].'';
                                    
                                    $resultHistorico = $pdo->select($Query);
                                    if ($resultHistorico) {
                                        foreach($resultHistorico as $rowHistorico){
                                            echo '		<tr>';
                                            echo '			<td width = "20%">'.$rowHistorico['dataMovimentacao'].'</td>';
                                            echo '			<td width = "80%">'.utf8_encode(substr($rowHistorico['observacoes'], 0, 100)).'</td>';
                                            echo '		</tr>';
                                        }
                                }?>
                            </tbody>
                        </table>
                        <form name="frmHistorico" id="frmHistorico" method="POST">
                            <table>
                                <tr>
                                    <td> Observa&ccedil;&otilde;es :
                                    <input type="hidden" name="ca_id" id="ca_id" value="<?php echo @$result['idAtendimento']; ?>"/></td>
                                    <td> <textarea name="ca_observacoes" id="ca_observacoes" class="input-xxlarge" title="Campo OBSERVAÇÕES deve ser preenchido." size="400" required ></textarea></td>
                                </tr>
                                <tr>
                                    <td> <button type="submit" name="gravaHistorico" class="btn btn-success"> <i class="icon-refresh"></i> Gravar </button>
                                    <button type="button" name="cancelar" onclick="apagaForm()" class="btn btn-danger" > <i class="icon-ban-circle"></i> Cancelar </button>
                                </td>
                                </tr>
                        </table>
                    </form>
                    </fieldset>
                </div>
        <?php
            }
            if(isset($_POST['id'])) {
                echo '<td colspan=4 align="right"> <button type="button" name="processo" id="processo" onclick="processo()" class="btn btn-info" > <i class="icon-plus"></i> Processo </button> </td>';
            } else {
                echo '<td colspan=4> <input type="checkbox" name="cadastrarProcesso" id="cadastrarProcesso" value="1"> Na sequência deseja cadastrar o PROCESSO do Cliente? </td>';
            }
        ?>
		<div id="retornoAtendimento"> </div>
		<div class="container">
				<h4>Favor preencha os campos abaixo citados:</h4>
				<ol>					
				</ol>
		</div>
	</body>
</html>